package com.beerq.view;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.beerq.R;
import com.beerq.util.AppConfig;
import com.beerq.util.BasicHttp;
import com.beerq.util.Constants;
import com.beerq.util.StringUtil;
import com.meiqia.core.callback.OnInitCallback;
import com.meiqia.meiqiasdk.util.MQConfig;
import com.meiqia.meiqiasdk.util.MQIntentBuilder;

import okhttp3.RequestBody;

public class ServicesActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        statusBarType=1;
        super.onCreate(savedInstanceState);
//        setMainContentView(R.layout.activity_services);
//        setBarTitle("留言");
//

        MQConfig.init(this, "d48d2b7bfef973ca1ef737a3b2e49f9f", new OnInitCallback() {
            @Override
            public void onSuccess(String clientId) {
                Intent intent = new MQIntentBuilder(ServicesActivity.this).build();
                startActivity(intent);
                finish();
            }

            @Override
            public void onFailure(int code, String message) {
                Toast.makeText(mContext, "int failure", Toast.LENGTH_SHORT).show();
            }
        });




//        ImageView imageView=(ImageView)findViewById(R.id.img_right);
//        imageView.setVisibility(View.VISIBLE);
//        imageView.setImageDrawable(getResources().getDrawable(R.drawable.call));
//        imageView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+"400-654-6900"));
//                startActivity(intent);
//            }
//        });
//
//        bindEvent();

    }

    private void bindEvent(){

        final EditText editName=(EditText)findViewById(R.id.edit_name);
        final EditText editPhone=(EditText)findViewById(R.id.edit_phone_number);
        final EditText editEmail=(EditText)findViewById(R.id.edit_email);
        final EditText editContent=(EditText)findViewById(R.id.edit_content);


        findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(StringUtil.isEmpty(editName.getText().toString())){
                    showToast("称呼不能为空！");
                    return;
                }

                String phoneNo=editPhone.getText().toString();
                if(StringUtil.isEmpty(phoneNo)){
                    showToast("手机号不能为空！");
                    return;
                }

                if(!phoneNo.startsWith("1") || phoneNo.length()!=11){
                    showToast("手机号码格式不正确！");
                    return;
                }
                if(StringUtil.isEmpty(editContent.getText().toString())){
                    showToast("留言内容不能为空！");
                    return;
                }

                JSONObject jsonObject=new JSONObject();
                jsonObject.put("Emaill",editEmail.getText().toString());
                jsonObject.put("MessageContent",editContent.getText().toString());
                jsonObject.put("MobileNo",phoneNo);
                jsonObject.put("NickName",editName.getText().toString());

                RequestBody requestBodyPost= AppConfig.getBasicRequestBody(mContext).add("Args",jsonObject.toJSONString()).build();
//
                BasicHttp.postExec(mContext, Constants.server_domain + "Vip/AddMessage", requestBodyPost, new BasicHttp.IMyCallBack() {
                    @Override
                    public void onResponse(String resonseData) {
                        showToast("提交成功！");
                        mHandler.sendEmptyMessageDelayed(0,1200);
                    }

                    @Override
                    public void onFailure(String resonseData) {

                    }
                });


            }



        });
    }

    Handler mHandler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if(msg.what==0){
                finish();
            }
            return false;
        }
    });
}
